#include "wxsimage.h"
#include "../properties/wxsimagelisteditordlg.h"

namespace {
  wxsRegisterItem<wxsImage> Reg( _T( "Image" ), _T( "Image" ), wxsTTool, _T( "工具" ), 73 );
}

wxsImage::wxsImage( wxsItemResData *Data ) :
  wxsTool( Data, &Reg.Info, 0, 0 ) {
  int n;
  wxString ss, tt;
  wxFileName fn;
  m_IsBuilt = false;
  m_ImageData.Clear();
  m_Include = false;
  fn = Data->GetSrcFileName();
  ss = fn.GetPath( ( wxPATH_GET_VOLUME + wxPATH_GET_SEPARATOR ) );
  n  = ss.Len();
  ss = ss + _T( "wximages" ) + ss[n - 1];
  m_IDir = ss;
  m_RDir = _T( "./wximages/" );
  m_Base = fn.GetName();
}

void wxsImage::OnBuildCreatingCode() {
  wxString inc;
  wxString vname;
  wxString bname;
  wxString xname;
  wxString ss, tt;
  if( m_IsBuilt ) {
    return;
  }
  m_IsBuilt = true;
  switch( GetLanguage() ) {
    case wxsCPP: {
      vname = GetVarName();
      bname = vname + _T( "_BMP" );
      xname = vname + _T( "_XPM" );
      AddHeader( _T( "<wx/image.h>" ), GetInfo().ClassName, 0 );
      AddHeader( _T( "<wx/bitmap.h>" ), GetInfo().ClassName, 0 );
      StoreXpmData();
      if( m_ImageData.Count() == 0 ) {
        Codef( _T( "%s = new wxImage();\n" ), vname.wx_str() );
        Codef( _T( "%s = new wxBitmap();\n" ), bname.wx_str() );
      } else {
        Codef( _T( "%s = new wxImage(%s);\n" ),  vname.wx_str(), xname.wx_str() );
        Codef( _T( "%s = new wxBitmap(%s);\n" ), bname.wx_str(), xname.wx_str() );
      }
      BuildSetupWindowCode();
      return;
    }
    case wxsUnknownLanguage:
    default: {
      wxsCodeMarks::Unknown( _T( "wxsImage::OnBuildCreatingCode" ), GetLanguage() );
    }
  }
}

void wxsImage::OnEnumToolProperties( M_Code_unused long Flags ) {
  m_IsBuilt = false;
  m_Context = GetCoderContext();
  WXS_IMAGE( wxsImage, m_ImageData, _T( "Image" ), _T( "image" ) );
  WXS_ARRAYSTRING( wxsImage, m_ImageData, _T( "Image as Text" ), _T( "image_text" ), _T( "item2" ) );
  WXS_BOOL( wxsImage, m_Include, _T( "Use Include File" ), _T( "use_include" ), false );
};


void wxsImage::OnBuildDeclarationsCode() {
  wxString    vname;
  wxString    bname;
  vname = GetVarName();
  bname = vname + _T( "_BMP" );
  AddDeclaration( _T( "wxImage               *" ) + vname + _T( ";" ) );
  AddDeclaration( _T( "wxBitmap              *" ) + bname + _T( ";" ) );
}

void wxsImage::StoreXpmData( void ) {
  int                 i, n;
  wxString    vname;
  wxString    xname;
  wxString    ss, tt;
  wxFile          ff;
  if( m_ImageData.Count() == 0 ) {
    return;
  }
  vname = GetVarName();
  xname = vname + _T( "_XPM" );
  tt = _T( "" );
  n = m_ImageData.GetCount();
  if( n > 5 ) {
    n = ( n * m_ImageData.Item( n - 2 ).Length() ) + 100;
    tt.Alloc( n );
  }
  for( i = 0; i < ( int )m_ImageData.GetCount(); i++ ) {
    ss = m_ImageData.Item( i );
    if( ss.Find( _T( "xpm_data" ) ) >= 0 ) {
      ss.Replace( _T( "xpm_data" ), xname );
    }
    tt += ss;
    tt += _T( "\n" );
  }
  if( m_Include ) {
    if( ! wxFileName::DirExists( m_IDir ) ) {
      wxFileName::Mkdir( m_IDir );
    }
    ss  = m_IDir;
    ss += m_Base;
    ss += _T( "_" );
    ss += xname;
    ss += _T( ".xpm" );
    ff.Open( ss, wxFile::write );
    ff.Write( tt );
    ff.Close();
    ss = _T( "\"" );
    ss += m_RDir;
    ss += m_Base;
    ss += _T( "_" );
    ss += xname;
    ss += _T( ".xpm" );
    ss += _T( "\"" );
    AddHeader( ss, GetInfo().ClassName, 0 );
  } else {
    Codef( tt );
  }
}

wxBitmap wxsImage::GetPreview( void ) {
  wxBitmap    bmp;
  if( m_ImageData.GetCount() == 0 ) {
    return wxNullBitmap;
  }
  wxsImageListEditorDlg::ArrayToBitmap( m_ImageData, bmp );
  return bmp;
}

void wxsImage::DoBuild( void ) {
  BuildCode( m_Context );
}
